import 'package:flutter/material.dart';
import 'package:hxdj/common/components/my_alert.dart';
import 'package:hxdj/common/components/my_tap_widget.dart';
import 'package:hxdj/common/components/my_text.dart';
import 'package:hxdj/common/my_header_global.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:package_info_plus/package_info_plus.dart';

class MyUpgradeChecker {
  static Future<void> checkForUpgrade(BuildContext context) async {
// 获取当前应用的版本信息
    final PackageInfo packageInfo = await PackageInfo.fromPlatform();
    // 这里只是一个示例，你需要替换为你的应用版本检查逻辑
    // 例如，你可以从服务器获取最新版本信息，或者检查应用商店的版本
    //去服务器请求最新的版本号
    Map<String, dynamic> response = await DioHttpManager().request(MyApi.version, method: DioHttpManager.post);
    if (response['code'] == 0) {
      Map<String, dynamic> responseMap = response["data"];
      Map<String, dynamic> listMap = responseMap["list"];
      Map<String, dynamic> iosMap = listMap["Ios"];

      String editionName = iosMap["edition_name"];
      editionName = editionName.replaceAll(".", '');

      String currentVersion = packageInfo.version.replaceAll(".", '');
      int currentV = int.parse(currentVersion);

      if (currentV < int.parse(editionName)) {
        // 检测到新版本，显示升级对话框或通知
        showUpgradeDialog(context, iosMap);
      } else {
        // 当前版本是最新的
        debugPrint("You are already on the latest version!");
      }
    }

    /**
   * 0:
"edition" -> 1
1:
"edition_name" -> "1.0.2"
2:
"content" -> "欢心到家"
3: 
"is_update" -> 1 1不强更2强更3线上调试模式
   */
  }

  static void showUpgradeDialog(BuildContext context, Map<String, dynamic> contentMap) {
    //获取屏幕的宽
    double screenWidth = MediaQuery.of(context).size.width;

    int isUpdate = contentMap['is_update'];
    Widget customWidget = Center(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          isUpdate == 2
              ? Container()
              : MyTapWidget(
            onTap: () {
              Navigator.pop(context);
            },
            child: Container(
              padding: EdgeInsets.only(right: 20.w),
              alignment: Alignment.centerRight,
              child: MyImage(
                imageName: "ic_cancel_upgrade",
                width: 20.w,
              ),
            ),
          ),
          Container(
              width: screenWidth - 104.w, // 可以根据需要设置容器的宽度
              height: 185.h, // 可以根据需要设置容器的高度
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/ic_upgrade.png'), // 使用 AssetImage 加载本地图片资源
                  fit: BoxFit.cover, // 图片填充方式，cover 表示等比例填充整个容器
                ),
              ),
              child: Padding(
                padding: EdgeInsets.only(top: 50.h),
                child: Column(
                  children: [
                    //title
                    MyText("版本更新", fontSize: 18.sp, fontWeight: FontWeight.bold, color: MyColours.text),
                    //content
                    Padding(
                      padding: EdgeInsets.only(top: 24.h),
                      child: MyText(contentMap["content"] ?? "",
                          fontSize: 13.sp, fontWeight: FontWeight.bold, color: MyColours.text),
                    ),

                    //按钮
                    Container(
                      margin: EdgeInsets.only(top: 25.h, right: 20.w, left: 20.w),
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 8.h),
                        decoration: BoxDecoration(
                          color: MyColours.appMain,
                          //设置四周圆角 角度
                          borderRadius: BorderRadius.circular(20.w),
                        ),
                        alignment: Alignment.center,
                        child: Text("马上更新",
                            style: TextStyle(fontSize: 15.sp, color: Colors.white, fontWeight: FontWeight.w700)),
                      ),
                    ),
                  ],
                ),
              )),
        ],
      ),
    );

    MyAlertManager.showAlert(AlertType.customWidget, context, "", "", child: customWidget);
  }

  static void launchAppStore() async {
    // 这里是跳转到应用商店的示例，你需要替换为你的应用的应用商店链接
    // if (Platform.isAndroid) {
    //   await launch("https://play.google.com/store/apps/details?id=your.app.package");
    // } else if (Platform.isIOS) {
    //   await launch("https://apps.apple.com/app/your-app-name/idYourAppId");
    // }
  }
}
